package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.UserLocationApi;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fone
 * @date 2021/09/05
 */

@Service
public class UserService {

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private UserLocationApi userLocationApi;



    public ResponseEntity findByPage(Integer pageNum, Integer pageSize) {

        IPage<UserInfo> iPage = userInfoApi.findUsers(pageNum, pageSize);

        //返回pageBeanVo(UserInfo数据)
        PageBeanVo pageBeanVo = new PageBeanVo(pageNum, pageSize, iPage.getTotal(), iPage.getRecords());


        return ResponseEntity.ok(pageBeanVo);

    }

    /**
     * 根据用户id查询坐标
     * @param userId
     * @return
     */
    public ResponseEntity findLocation(Long userId) {

        UserLocationVo userLocationVo = userLocationApi.findLocationByUserId(userId);

        Map map = new HashMap(5);

        boolean state = false;

        String message = "该用户还未上传地理信息";

        if (userLocationVo != null) {
            state = true;
            message = "success";
            map.put("longitude", userLocationVo.getLongitude());
            map.put("latitude", userLocationVo.getLatitude());
            map.put("address", userLocationVo.getAddress());

        }
        map.put("state", state);
        map.put("message", message);

        return ResponseEntity.ok(map);
    }
}
